<template>
  <div class="back-top" @click="backTop" v-show="isshow">
    <div class="arrow"></div>TOP
  </div>
</template>

<script>
export default {
  data() {
    return {
      isshow: false
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
  },
  methods: {
    backTop() {
      let timer = setInterval(() => {
        document.documentElement.scrollTop = 0
        clearInterval(timer)
      },0)
      this.isshow = false
      
    },
    handleScroll() {
      let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      const target = 500
      if(scrollTop > target) {
        this.isshow = true
      }else {
        this.isshow = false
      }
    }
  }
}
</script>

<style scoped>
.back-top {
  position: relative;
  position: fixed;
  bottom: 50px;
  right: 50px;
  width: 30px;
  height: 35px;
  padding-top: 18px;
  text-align: center;
  font-size: 12px;
  color: #555;
  background-color: #fff;
  cursor: pointer;
}
.arrow {
  position: absolute;
  left: 9px;
  top: -7px;
  width: 12px;
  height: 22px;
  box-sizing: border-box;
  border-width: 11px 6px;
  border-style: solid;
  border-color: transparent transparent #242424 transparent;
}
</style>